package com.android.love.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

public class MyScrollView extends ScrollView {
	private Runnable scrollerTask;
	private int initialPosition;

	private int newCheck = 100;
	private static final String TAG = "MyScrollView";

	public interface OnScrollStoppedListener {
		void onScrollStopped();
		
		void onScrolling();
	}

	public interface OnScrollUpDownListener {
		void onScrollUp();

		void onScrollDown();
	}

	private OnScrollStoppedListener onScrollStoppedListener;
	private OnScrollUpDownListener onScrollUpDownListener;

	public MyScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);

		scrollerTask = new Runnable() {

			public void run() {

				int newPosition = getScrollY();
				if (initialPosition - newPosition == 0) {// has stopped

					if (onScrollStoppedListener != null) {

						onScrollStoppedListener.onScrollStopped();
					}
				} else {
					onScrollStoppedListener.onScrolling();
					initialPosition = getScrollY();
					MyScrollView.this.postDelayed(scrollerTask, newCheck);
				}
			}
		};
	}

	public void setOnScrollStoppedListener(
			MyScrollView.OnScrollStoppedListener listener) {
		onScrollStoppedListener = listener;
	}

	public void setOnScrollUpDownListener(
			MyScrollView.OnScrollUpDownListener listener) {
		onScrollUpDownListener = listener;
	}

	public void startScrollerTask() {

		initialPosition = getScrollY();
		MyScrollView.this.postDelayed(scrollerTask, newCheck);
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);

//		if (l > oldt) {
//			// onScrollDownListener.onScrollDown();
//			onScrollUpDownListener.onScrollDown();
//		} else {
//			onScrollUpDownListener.onScrollUp();
//		}
	}

}
